package com.kyrie.lib_sys.utils.learn.TimeDemo;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class TimeDemo06 {

    // 定义日期时间格式化器
    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    /**
     * 检查任务列表中时间的顺序和重叠情况
     * @param timeList 时间集合，每个时间集合包含一系列时间点(4个)
     * @return 如果所有timeList集合的时间顺序正确且没有重叠，则返回true；否则返回false
     */
    public static boolean timeListCheck(List<Map<String, String>> timeList) {
        // 存储所有任务的时间区间
        List<TimeInterval> allTimes = new ArrayList<>();

        // 遍历每个timeList时间集合,
        for (int i = 0; i < timeList.size(); i++) {
            Map<String, String> taskTime = timeList.get(i);
            List<LocalDateTime> timePoints = new ArrayList<>();

            // 获取里面每一个时间点,放入一个全局时间timePoints列表中
            if (taskTime.containsKey("prepareTime")) timePoints.add(LocalDateTime.parse(taskTime.get("prepareTime"), formatter));
            if (taskTime.containsKey("travelTime")) timePoints.add(LocalDateTime.parse(taskTime.get("travelTime"), formatter));
            if (taskTime.containsKey("startTime")) timePoints.add(LocalDateTime.parse(taskTime.get("startTime"), formatter));
            if (taskTime.containsKey("completeTime")) timePoints.add(LocalDateTime.parse(taskTime.get("completeTime"), formatter));

            //校验timeList时间顺序
            for (int j = 1; j < timePoints.size(); j++) {
                if (!timePoints.get(j - 1).isBefore(timePoints.get(j))) {
                    System.out.printf("当前时间点: \"%s\" 存在时间顺序错误\n", timePoints.get(j).format(formatter));
                    return false;
                }
            }

            // 2. 构建时间段（最早时间到最晚时间）
            LocalDateTime start = Collections.min(timePoints);
            LocalDateTime end = Collections.max(timePoints);
            TimeInterval currentInterval = new TimeInterval(start, end);

            // 3. 时间重叠校验
            for (TimeInterval existing : allTimes) {
                if (currentInterval.overlaps(existing)) {
                    System.out.printf("当前时间点: \"%s\" 存在时间重叠错误\n", currentInterval.start.format(formatter));
                    return false;
                }
            }

            allTimes.add(currentInterval);
        }

        return true;
    }

    /**
     * 表示时间区间的类
     */
    static class TimeInterval {
        /**
         * 时间区间的开始时间a
         */
        LocalDateTime start;
        /**
         * 时间区段的结束时间
         */
        LocalDateTime end;

        TimeInterval(LocalDateTime start, LocalDateTime end) {

            this.start = start;
            this.end = end;
        }

        /**
         * 判断两个时间点是否重叠
         * @param other 另一个时间区间
         * @return 如果重叠返回true，否则返回false
         */
        boolean overlaps(TimeInterval other) {
            return !(this.end.isBefore(other.start) || this.start.isAfter(other.end));
        }
    }




    //下面是测试的功能

    //1: 单个timeList的时间校验 (4个时间点可以输入一个或者多个,不影响校验功能)


    //2: 多个timeList的时间校验 (4个时间点可以输入一个或者多个)


    //3: 当存在出现时间重叠冲突时  返回第一个错误时间区间
    public static void main(String[] args) {
        List<Map<String, String>> timeList = new ArrayList<>();



      //测试用例1 (单个map集合间的校验)
      Map<String, String> timeList0 = new HashMap<>();
      timeList0.put("prepareTime", "2025-07-01 09:00:00");
      timeList0.put("travelTime", "2025-07-01 09:10:00");
      //timeList0.put("startTime", "2025-07-01 09:20:00");
      timeList0.put("completeTime", "2025-07-01 09:30:00");

      timeList.add(timeList0);






        //测试用例2 (多个map集合间的校验)

        Map<String, String> timeList1 = new HashMap<>();
        timeList1.put("prepareTime", "2025-07-01 09:40:00");
        timeList1.put("travelTime", "2025-07-01 09:45:00");
        timeList1.put("startTime", "2025-07-01 09:50:00");
        timeList1.put("completeTime", "2025-07-01 09:55:00");

        Map<String, String> timeList2 = new HashMap<>();
        timeList2.put("prepareTime", "2025-07-01 10:00:00");
        timeList2.put("travelTime", "2025-07-01 10:10:00");
        timeList2.put("startTime", "2025-07-01 10:20:00");
        timeList2.put("completeTime", "2025-07-01 10:30:00");


        timeList.add(timeList1);
        timeList.add(timeList2);


        boolean result = timeListCheck(timeList);
        System.out.println("校验结果: " + result);
    }




}
